
#metadef
#idString Kunkin,Kunkin KP184 V4
#name Kunkin KP184 V4
#handle KP184
#subDriver Kunkinx
#sections NoAHWH

#metadef
#idString Kunkin,Kunkin KP184 V4+
#name Kunkin KP184 V4+
#handle KP184
#subDriver Kunkinx
#sections AHWH



#metadef
#sections NoAHWH

#meta
#author GBY, MikeLud, HKJ 
#idString Kunkin,Kunkin KP184
#name Kunkin KP184
#handle KP184
#port com
#driver Modbus
#subDriver Kunkin


#notes KP184 changed the protocol in V4, for this reason there are two version of this driver. The V4+ supports a software update that includes Ah & Wh

#scpiCmd model? holdingL? 0x0001
#verifyDevice 1840 model?

#scpiCmd on holdingL 0x010E (value)
;Need to mask reading on/off status.  Sometimes reports 0xA000000 plus 0 or 1.  No idea why.
#scpiCmd on? holdingL? 0x010E
:readmath: (0x1&value)

#scpiCmd mode holdingL 0x0110 (value)
;Need to mask reading remote status.  Sometimes reports 0x10000002 plus 0-3.  No idea why.
#scpiCmd mode? holdingL? 0x0110
:readmath: (0x3&value)

#scpiCmd voltage holdingL 0x0112 (value) *1000
#scpiCmd voltage? holdingL? 0x0112 /1000
#scpiCmd current holdingL 0x0116 (value) *1000
#scpiCmd current? holdingL? 0x0116 /1000
#scpiCmd resistance holdingL 0x011A (value)
#scpiCmd resistance? holdingL? 0x011A
#scpiCmd power holdingL 0x011E (value) *10
#scpiCmd power? holdingL? 0x011E /10
#scpiCmd remote holdingL 0x10a (value)
;Need to mask reading remote status.  Sometimes reports 0xA000000 plus 0 or 1.  No idea why.
#scpiCmd remote? holdingL? 0x10a
:readmath: (0x1&value)

#scpiCmd VoltageM? holdingL? 0x0122 /1000
#scpiCmd CurrentM? holdingL? 0x0126 /1000

#value Voltage V D3 
#value Current A D3 
#metaSection AHWH
#value Capacity Ah D6
#value Energy Wh D3 
#metaSection


; This is a single line command
#metaSection NoAHWH
#askValues holdingL? 0x0300 8 /1000
;#askValues holdingL? 0x0122 /1000;holdingL? 0x0126 /1000
#metaSection AHWH
#askValues holdingL? 0x0301 8 /1000 /1000 /3600000 /3600000
#metaSection

; Initial commands to meter when establishing connection
#initCmd  mode?


; Final command to meter before breaking connection
#finalCmd on 0

; Used when output off button is pressed
#outputOff on 0

; This type will specify the handle name for the first device with this type
; This makes it possible to easy get generic handle names for a setup, that will work with a script
#interfaceType Load
; These function will adjust settings
#interface setVoltage voltage (value)
#interface setCurrent current (value)
#interface setPower power (value)
#interface setResistance resistance (value)
#interface setOn on (value)
#interface setMode mode (value)
#interface setRemoteSense remote (value)
; These function will return the value applied with the set function
#interface getVoltage voltage?
#interface getCurrent current?
#interface getPower power?
#interface getResistance resistance?
#interface getOn on?
#interface getMode mode?
#interface getRemoteSense remote?
; These functions will read the actual values
; These can either contain a number that will reference to a data column or they can contain a command to read the value
#interface readVoltage 0
#interface readCurrent 1


#cmdsetup buttonsOn Status
:read: on?
:write: on
:update: Mode
off 0
on 1

#cmdSetup radio Mode
:read: mode?
:write: mode
CC 1
CV 0
CR 2
CW 3

#cmdSetup radio Current
:read: current?
:write: current 
:update: Mode Current
:tip: Load current for current mode
100mA 0.1
300mA 0.3
1A 1.0
3A 3.0
10A 10.0
40A 40.0

#cmdSetup number Current
:read: current?
:write: current 
:update: Mode
:tip: Load current for current mode
:buttontext: CC
Amps 0 40.000

#cmdSetup number Voltage
:read: voltage?
:write: voltage 
:update: Mode
:tip: Load voltage for voltage mode
:buttontext: CV
Volt 0 150.000

#cmdSetup number Resistance
:read: resistance?
:write: resistance 
:tip: Load resistance for resistance mode
:buttontext: CR
Ohm 0 99998

#cmdSetup number Power
:read: power?
:write: power 
:update: Mode
:tip: Load power for power mode
:buttontext: CW
Watt 0 400.0

#cmdSetup checkbox Remote_sense Main
:read: remote?
:write: remote
:update: remote?
on 0 1


;; KP184: Tx <holding? 0x0301>
;; COM12: Tx: 01 03 03 01 00 02 95 8F
;; COM12: Rx: 01 03 1F 0B 04 00 2A CA 00 19 64 00 19 64 00 FA 00 FA 00 00 00 00 00 00 00 0A 8C 00 0C A0 6C 00 8A 00 A8 46
;; KP184: Rx Timeout

; 01 03 1F 0B 04 00 2A CA 00 19 64 00 19  64  00  FA  00  FA  00  00  00   00  00  00  00  0A  8C  00  0C  A0  6C  00  8A  00  A8  46   ????
;(01 03 1F D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12 D13 D14 D15 D16 D17 D18) D19 D20 D21 D22 D23 D24 D25 D26 D27 D28 D29 D30 D31 D32 CRCH CRCL
;|-- -- --|--|--|voltage-|current-|-- --  -- |--  -- |--  --  -- |--  --  |-- |-- |-- |voltage end|--- -- mAs  ---|--- --mWs-- ---|--  |--  |
;|-- -- --|--|--|10954mV-|6500mA--|-- --  -- |--  -- |--  --  -- |--  --  |-- |-- |-- |--2700mV---|---827500mAs---|---9044136mWs--|--  |--  |
